package edu.biwu.mapper;

import edu.biwu.po.Order;
import edu.biwu.po.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface OrderMapper {
    /**
     * 查询所有订单(按需加载订单关联的用户信息)
     * //@One相当于<association></association>
     * @return
     */
    @Results({
            @Result(column = "user_id",property = "userId"),
            @Result(property = "user",javaType = User.class,column = "user_id",
            one = @One(select = "edu.biwu.mapper.UserMapper.findUserById",fetchType = FetchType.LAZY))
    })
    @Select("SELECT id,user_id,number,createtime,note FROM mybatis02.order")
    List<Order> findOrderAndUserLazy();

    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "user_id",property = "userId"),
            @Result(property = "user",javaType = User.class,column = "user_id",
                    one = @One(select = "edu.biwu.mapper.UserMapper.findUserById",fetchType = FetchType.LAZY)),
            @Result(property = "orderDetails",javaType = List.class,column = "id",
            many=@Many(select = "edu.biwu.mapper.OrderDetailMapper.findByOrderId",fetchType = FetchType.LAZY))
    })
    @Select("SELECT id,user_id,number,createtime,note FROM mybatis02.order")
    List<Order> findOrderAndOrderDetailLazy();

    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(property = "orderDetails",javaType = List.class,column = "id",
            many=@Many(select = "edu.biwu.mapper.OrderDetailMapper.findByOrderId",fetchType = FetchType.LAZY))
    })
    @Select("SELECT id,number,createtime,note FROM mybatis02.order WHERE user_id = #{userId}")
    List<Order> findByUserId(int userId);
}
